﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Reuse.PCL.Models;
using System;

namespace Reuse.PCL.Extensions {
	/// <summary>
	/// Sends a MVVM Light Message using a Message Struct to encapsulate a command and value to identify the messages in case of multiple requests.
	/// </summary>
	public static class MessengerHelper {
		/// <summary>
		/// Sends a MVVM Light Message
		/// </summary>
		/// <param name="sender">The ViewModelBase class from which the message is sent</param>
		/// <param name="command">The command string to identify the message type</param>
		/// <param name="value">The value to send</param>
		/// <param name="token">The token to identify who can receive the message</param>
		public static void SendMessage( this ViewModelBase sender, string token, string command, object value = null  ) {
			Messenger.Default.Send<MessageStruct>( new MessageStruct() { Command = command, Value = value }, token );
		}

		/// <summary>
		/// Subscribes an object to a MVVM Light Message
		/// </summary>
		/// <param name="receiver">The object receiving the message</param>
		/// <param name="token">The token that identifies who can receive the message</param>
		/// <param name="handler">Handler when the message is received</param>
		public static void SubscribeToMessage( this object receiver, string token, Action<MessageStruct> handler ) {
			Messenger.Default.Register<MessageStruct>( receiver, token, handler );
		}
	}
}
